//
//  MainViewController.m
//  ENTBoostChat
//
//  Created by zhong zf on 14-8-7.
//  Copyright (c) 2014年 EB. All rights reserved.
//

#import "MainViewController.h"
#import "TalksTableViewController.h"
#import "RelationshipsViewController.h"
#import "BlockUtility.h"
#import "AppDelegate.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSArray* controllers = self.childViewControllers;
    UINavigationController* firstNavController = controllers[0];
    UINavigationController* secondNavController = controllers[1];
    
    //解决导航栏覆盖内容的问题
    firstNavController.navigationBar.translucent = NO;
    secondNavController.navigationBar.translucent = NO;
    
    //获取tarbar里各个控制器
    self.talksController = firstNavController.viewControllers[0];
    self.talksController.tabBarController = self;
    self.relationshipController = secondNavController.viewControllers[0];
    self.relationshipController.tabBarController = self;
    
    //设置tabbar图标及标题
    UITabBar *tabBar = self.tabBar;
    
    UITabBarItem *tabBarItem  = [tabBar.items objectAtIndex:0];
    [tabBarItem setTitle:@"聊天"];
    [tabBarItem setImage:[UIImage imageNamed:@"talks"]];
    
    UITabBarItem *tabBarItem1  = [tabBar.items objectAtIndex:1];
    [tabBarItem1 setTitle:@"联系人"];
    [tabBarItem1 setImage:[UIImage imageNamed:@"relationship"]];
    
//    [trainEnquiryItem setSelectedImage:[UIImage imageNamed:@"tet_hover"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ENTBoostKitDelegate
- (void)onRecevieMessage:(EBMessage*)message
{
    [self.talksController dispatchReceviedMessage:message];
}

- (void)onCallConnected:(const EBCallInfo *)callInfo
{
    [self.talksController handleCall:callInfo callActionType:CALL_ACTION_TYPE_CONNECTED];
}

- (void)onCallBusy:(const EBCallInfo *)callInfo
{
    [self.talksController handleCall:callInfo callActionType:CALL_ACTION_TYPE_BUSY];
}

- (void)onCallReject:(const EBCallInfo *)callInfo
{
    [self.talksController handleCall:callInfo callActionType:CALL_ACTION_TYPE_REJECT];
}

- (void)onCallHangup:(const EBCallInfo *)callInfo
{
    [self.talksController handleCallHangup:callInfo];
}

- (void)onCallAlerting:(const EBCallInfo *)callInfo toUid:(uint64_t)toUid
{
    [self.talksController handleCallAlerting:callInfo toUid:toUid];
}

- (void)onCallIncoming:(const EBCallInfo *)callInfo fromUid:(uint64_t)fromUid fromAccount:(NSString *)fromAccount vCard:(EBVCard *)vCard clientAddress:(NSString *)clientAddress
{
    [self.talksController handleCallIncoming:callInfo fromUid:fromUid fromAccount:fromAccount vCard:vCard clientAddress:clientAddress];
}

- (void)onAutoLogonEvent:(BOOL)state accountInfo:(EBAccountInfo *)accountInfo error:(NSError *)error
{
    [BlockUtility performBlockInMainQueue:^{
        AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        if (state) {
            //把MainViewController设置为根控制器
            appDelegate.window.rootViewController =self;
        } else {
            //把LogonViewController设置为根控制器
            LogonViewController* logonViewController = appDelegate.logonViewController;
            appDelegate.window.rootViewController = logonViewController;
            logonViewController.loginButton.enabled = YES;
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
